﻿#include("../../layout/header.html")
#@layout()
#define content()
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字:</label>
                        <div class="layui-input-inline">
                            <input name="search" class="layui-input" placeholder="输入角色名、代码、备注"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">数据权限:</label>
                        <div class="layui-input-inline">
                            <select id="permissionType" name="permissionType">
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                        <!--<button class="layui-btn layui-btn-warm icon-btn" type="reset">
                            <i class="layui-icon layui-icon-refresh"></i>清空筛选
                        </button>-->
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="auth">权限分配</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">数据权限:</label>
            <div class="layui-input-block">
                <select id="permissionType2" name="permissionType" lay-filter="permissionType">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色名:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入角色名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色代码:</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入角色代码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">优先级:</label>
            <div class="layui-input-block">
                <input name="priority" placeholder="数字越大代表优先级越大" class="layui-input" type="number" value="1"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remarks" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>

        <div id="diyDiv" class="layui-form-item layui-hide">
            <label class="layui-form-label">自定义</label>
            <div class="layui-input-block">
                <input type="checkbox" name="strict" lay-filter="strict" lay-skin="primary" title="父子联动" checked>
            </div>
            <div class="layui-input-block">
                <div id="orgs"></div>
            </div>
        </div>

        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree','formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var formX = layui.formX;
        var admin = layui.admin;
        var insOrgSel;
        var permissionType = [],orgs = [];
        admin.req('/sys/sysDictData/get',{type:'permissionType'},function (d) {
            if (0 === d.code) {
                permissionType = d.data.permissionType;
                formX.renderSelect({
                    elem: '#permissionType',
                    data: permissionType,
                    name: 'name',
                    value: 'value',
                    hint: '请选择数据权限范围'
                });
            }
        })
        admin.req('/sys/sysOrg/selectData',{},function (d){if(0 === d.code){orgs = d.data;}})

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#roleTable',
            url: '/sys/sysRole/list',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'name', title: '角色名', sort: true},
                {field: 'code', title: '角色代码', sort: true},
                {field: 'permissionTypeStr', title: '数据权限'},
                {field: 'remarks', title: '备注'},
                {
                    field: 'createTime', title: '创建时间', sort: true
                },
                {title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(roleTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermModel(obj.data.id);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('roleTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {

            admin.popupRight({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {

                    // 渲染搜索栏中的性别select
                    formX.renderSelect({
                        elem: '#permissionType2',
                        data: permissionType,
                        name: 'name',
                        value: 'value',
                        initValue: mData ? mData.permissionType : "",
                        hint: '请选择数据权限范围'
                    });

                    // 回显表单数据
                    form.val('roleEditForm', mData);

                    if(mData && mData.permissionType && 4 == mData.permissionType){
                        insOrgSel = formX.renderMultiTreeSelect('#orgs','dataScope',orgs);
                        if(mData && mData.dataScope){
                            insOrgSel.setValue(mData.dataScope.split(','))
                        }
                        $('#diyDiv').removeClass('layui-hide')
                    }


                    form.on('select(permissionType)', function(data){
                        if(data.value == 4){
                            insOrgSel = formX.renderMultiTreeSelect('#orgs','dataScope',orgs);
                            if(mData && mData.dataScope){
                                insOrgSel.setValue(mData.dataScope.split(','))
                            }
                            $('#diyDiv').removeClass('layui-hide')

                            form.on('checkbox(strict)', function(data){
                                if(data.elem.checked){
                                    insOrgSel = formX.renderMultiTreeSelect('#orgs','dataScope',orgs);
                                    if(mData && mData.dataScope){
                                        insOrgSel.setValue(mData.dataScope.split(','))
                                    }
                                }else {
                                    insOrgSel = formX.renderMultiTreeSelect('#orgs','dataScope',orgs,'off');
                                    if(mData && mData.dataScope){
                                        insOrgSel.setValue(mData.dataScope.split(','))
                                    }
                                }


                            });


                        }else {
                            $('#diyDiv').addClass('layui-hide')
                        }
                    });


                    // 表单提交事件
                    form.on('submit(roleEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        admin.post('/sys/sysRole/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/sysRole/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

        /* 权限管理 */
        function showPermModel(roleId) {
            admin.open({
                title: '角色权限分配',
                btn: ['保存', '取消'],
                content: '<ul id="roleAuthTree" class="ztree"></ul>',
                success: function (layero, dIndex) {
                    var loadIndex = layer.load(2);
                    admin.req('/sys/sysAuthorities/getAuthTree', {roleId: roleId}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            $.fn.zTree.init($('#roleAuthTree'), {
                                check: {enable: true},
                                data: {simpleData: {enable: true}}
                            }, res.data);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    });
                    // 超出一定高度滚动
                    $(layero).children('.layui-layer-content').css({'max-height': '600px', 'overflow': 'auto'});
                },
                yes: function (dIndex) {
                    var insTree = $.fn.zTree.getZTreeObj('roleAuthTree');
                    var checkedRows = insTree.getCheckedNodes(true);
                    var ids = [];
                    for (var i = 0; i < checkedRows.length; i++) {
                        ids.push(checkedRows[i].id);
                    }
                    var loadIndex = layer.load(2);
                    admin.req('/sys/sysAuthorities/addRoleAuth', {roleId: roleId, authIds: ids.join(',')}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(dIndex);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'post');
                }
            });
        }

    });
</script>
</body>
#end
